﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace FlickrSL.Data
{
    [XmlType("photo")]
    public class PhotoInfo
    {
        public PhotoInfo()
        {
            this.Tags = new List<Tag>();
            this.Urls = new List<Url>();
            this.Notes = new List<Note>();
        }

        [XmlAttribute("id")]
        public Int64 Id { get; set; }

        [XmlAttribute("secret")]
        public String Secret { get; set; }

        [XmlAttribute("isfavorite")]
        public Boolean IsFavorite { get; set; }

        [XmlAttribute("license")]
        public Int32 LicenseId { get; set; }

        [XmlAttribute("rotation")]
        public Int32 Rotation { get; set; }
        
        [XmlAttribute("originalsecret")]
        public String OriginalSecret { get; set; }

        [XmlAttribute("originalformat")]
        public String OriginalFormat { get; set; }

        [XmlAttribute("media")]
        public String MediaType { get; set; }

        [XmlElement("owner")]
        public OwnerInfo Owner { get; set; }

        [XmlElement("title")]
        public String Title { get; set; }

        [XmlElement("description")]
        public String Description { get; set; }

        [XmlElement("visibility")]
        public VisibilityInfo Visibility { get; set; }

        [XmlIgnore]
        public DateTime? DatePosted { get; set; }

        [XmlAttribute("dateuploaded")]
        public String DatePostedString
        {
            get
            {
                if (this.DatePosted.HasValue)
                {
                    return ((Int64)this.DatePosted.Value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds).ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    this.DatePosted = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Int64.Parse(value));
                }
                else
                {
                    this.DatePosted = null;
                }
            }
        }

        [XmlElement("dates")]
        public PhotoDates Dates { get; set; }

        [XmlElement("location")]
        public Location Location { get; set; }

        [XmlElement("geoperms")]
        public LocationPermissions LocationPermissions { get; set; }

        [XmlElement("permissions")]
        public PhotoPermissions Permissions { get; set; }

        [XmlElement("editability")]
        public PhotoEditability Editability { get; set; }

        [XmlElement("usage")]
        public UsageRights Usage { get; set; }

        [XmlElement("comments")]
        public Int32 CommentCount { get; set; }

        [XmlAttribute("views")]
        public Int32 ViewCount { get; set; }

        [XmlArray("notes")]
        [XmlArrayItem("note")]
        public List<Note> Notes { get; set; }

        [XmlArray("tags")]
        [XmlArrayItem("tag")]
        public List<Tag> Tags { get; set; }

        [XmlArray("urls")]
        [XmlArrayItem("url")]
        public List<Url> Urls { get; set; }
    }

    [XmlType("user")]
    public class OwnerInfo
    {
        [XmlAttribute("nsid")]
        public String UserId { get; set; }

        [XmlAttribute("username")]
        public String Username { get; set; }

        [XmlAttribute("realname")]
        public String RealName { get; set; }

        [XmlAttribute("location")]
        public String Location { get; set; }
    }
}
